﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;

using YiSha.Entity.YiDongYuan;

namespace YiSha.Service.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-16 08:31
    /// 描 述：机械备案申报服务类
    /// </summary>
    public class DetectionExportService : RepositoryFactory
    {
        #region 获取数据

        public async Task<List<DetectionExportEntity>> GetExportList(ExportListParam param)
        {
            //环保号牌  所有人 电话 所属县区 机械类型 机械名称 排放阶段 燃油类型 发动机功率
            //机械型号 机械出厂日期 检测日期 检测人 检测类型
            StringBuilder strSql = new StringBuilder(@"SELECT t.HuanBaoCode AS 环保号牌,m.OwnerName AS 所有人,m.OwnerTel AS 电话,
                           t.Region AS 所属县区,m.JXDJLX AS 机械类型,m.JXLB AS 机械名称,
                           m.PFJD AS 排放阶段, m.RLLX AS 燃油类型,m.FDJGL AS 发动机功率,
                           m.JXXH AS 机械型号,m.JXCCRQ AS 机械出厂日期,u.RealName AS 检测人,
						    DATE_FORMAT(t.DetectionTime, '%Y-%m-%d')  AS 检测日期,
                           CASE t.DetectionType WHEN 0 THEN '初检'  WHEN 1 THEN '治理复检' ELSE '常规复检' END AS 检测类型,
							case d.Result WHEN 0 THEN '合格' ELSE '不合格' END as 检测结果,
							case d.ResultDPQ WHEN 0 THEN '合格' ELSE '不合格' END as 低排区
                    FROM detectiontask t
                    left JOIN detectionmechanics m ON t.MechanicsId = m.Id
                    left JOIN sysuser u ON t.DetectionUserId = u.id
										left join detectioninfo  d on d.id=t.DetectionInfoId
                    where t.DetectionStatus=1 ");
            var parameter = new List<DbParameter>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.Region) && !param.Region.Equals("运城市"))
                {
                    strSql.Append("AND t.Region = @Region ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@Region", param.Region));
                }
                if (param.StartDate != null)
                {
                    strSql.Append("AND DATE_FORMAT(t.DetectionTime, '%Y-%m-%d %H-%i-%s')>= DATE_FORMAT(@StartDate,'%Y-%m-%d %H-%i-%s') ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@StartDate", param.StartDate));
                }
                if (param.EndDate != null)
                {
                    strSql.Append("AND DATE_FORMAT(t.DetectionTime, '%Y-%m-%d %H-%i-%s')<= DATE_FORMAT(@EndDate,'%Y-%m-%d %H-%i-%s') ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@EndDate", param.EndDate));
                }
            }
            strSql.Append("ORDER BY t.DetectionTime DESC");
            var list = await this.BaseRepository().FindList<DetectionExportEntity>(strSql.ToString(), parameter.ToArray());
            return list.ToList();
        }

        #endregion 获取数据
    }
}
